﻿
;;set emacs size and pos
(setq default-frame-alist 
'((height . 30) (width . 95) (top . 0) (left . 0) )) 
;;end 
;;set for codeing system
;; 设置默认编码
;;set utf8 code
;; (set-language-environment "UTF-8")
;; (set-terminal-coding-system 'utf-8)
;; (set-keyboard-coding-system 'utf-8)
;; (prefer-coding-system 'utf-8)
;; (setq default-buffer-file-coding-system 'utf-8)
;;set of setting utf8

(set-language-environment "Chinese-GB18030")
(setq file-name-coding-system 'gb18030)

;;set font
; Microsoft JhengHei / 微软雅黑 /
;    (set-fontset-font "fontset-default"
;    'gb18030 '("Microsoft YaHei" . "unicode-bmp"))
;;字体可以通过属性修改大小，如 :pixelsize=18:foundry=monotype:weight=medium:slant=i:width=normal
;;配置结果可以通过M-x describe-char获取字体信息

;; (if (eq system-type 'windows-nt) 
;;     (set-default-font "Courier New")
;; (set-default-font "Bitstream Vera Sans Mono")) 

(set-default-font "YaHei Consolas Hybrid-12")
;;好像不认识gb2312哦

(set-fontset-font "fontset-default" 'han '("Microsoft YaHei". "unicode-bmp"))
;;(frame-parameter nil 'font)获取的就是fontset-default的设置
(set-fontset-font (frame-parameter nil 'font)
        'cjk-misc '("Microsoft YaHei" . "unicode-bmp"))
(set-fontset-font (frame-parameter nil 'font)
        'bopomofo '("Microsoft YaHei" . "unicode-bmp"))
(set-fontset-font "fontset-default"
        'gb18030 '("Microsoft YaHei". "unicode-bmp"))
(set-fontset-font (frame-parameter nil 'font)
        'symbol '("Microsoft YaHei". "unicode-bmp")) 

 
;;设置主题
(require 'color-theme)
(color-theme-initialize)
(setq color-theme-is-global t)
(color-theme-robin-hood)
 
;;end of 主题
;;end set font
(autoload 'run-prolog "prolog" "Start a Prolog sub-process." t)
    (autoload 'prolog-mode "prolog" "Major mode for editing Prolog programs." t)
    (autoload 'mercury-mode "prolog" "Major mode for editing Mercury programs." t)
    (setq prolog-system 'swi)
    (setq auto-mode-alist (append '(("\\.pro$" . prolog-mode)
                                   ("\\.m$" . mercury-mode))
                                   auto-mode-alist))

;;回复桌面
;;############### Session.el ###############
(require 'session)
(add-hook 'after-init-hook 'session-initialize)
;;############### Desktop Reload ###############
;;(load "desktop")
(require 'desktop)
;;(add-to-list 'desktop-modes-not-to-save 'dired-mode)
;;(add-to-list 'desktop-modes-not-to-save 'Info-mode)
;;(add-to-list 'desktop-modes-not-to-save 'info-lookup-mode)
;;(add-to-list 'desktop-modes-not-to-save 'fundamental-mode)
;; use only one desktop
(setq desktop-path '("~/.emacs.d/"))
(setq desktop-dirname "~/.emacs.d/")
(desktop-load-default) 
(desktop-read)
(setq desktop-base-file-name "emacs-desktop")
(desktop-save-mode t)

;;多标签支持
(require 'tabbar) 
(setq tabbar-speedkey-use t)
(setq tabbar-speedkey-prefix (kbd "<f1>"))
(tabbar-mode 1)


;;配置perl
;(require 'cperl-mode)
;;(add-hook 'perl-mode-hook 'cperl-mode)

;;配置org-mode
;;(setq org-hide-leading-stars t) 
 (define-key global-map "\C-ca" 'org-agenda) 
 (setq org-log-done 'time)  



;;c-x c-m 调出输入命令框

(setq outline-minor-mode-prefix [(control o)]) ;;更改outline-minor-mode的命令前缀


;; 高亮结尾白空格 
;; 设置颜色 
(custom-set-faces
  ;; custom-set-faces was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(my-long-line-face ((((class color)) (:background "gray10"))) t)
 '(my-tab-face ((((class color)) (:background "grey10"))) t)
 '(my-trailing-space-face ((((class color)) (:background "gray10"))) t)
 '(org-level-2 ((t (:inherit yellow-face)))))
 
;; 辅助函数，用来给指定模式添加超过80列的高亮功能 

;; (defun cc-mode-add-keywords (mode) 
;;   (font-lock-add-keywords  
;;    mode 
;;    '(("\t+" (0 'my-tab-face append)) 
;;      ("^.\\{81\\}\\(.+\\)$" (1 'my-long-line-face append))))) 
;; ;; 对指定模式使用"超过80列高亮"功能 
;; (cc-mode-add-keywords 'c-mode) 
;; (cc-mode-add-keywords 'cc-mode) 
;; (cc-mode-add-keywords 'c++-mode) 
;; (cc-mode-add-keywords 'perl-mode) 
;; (cc-mode-add-keywords 'python-mode);;高亮设置结束

;;TODO  alt键目前不会表示
;;(define-prefix-command 'alt-map)


;;按ctrl + 键能增加emacs字体的大小





;;这段代码能够改变shell-mode中字体的大小
;; make a face
;;(make-face 'font-lock-small-face)
;;(set-face-attribute 'font-lock-small-face nil :height 1.5) ; have smaller font. 
;; add this setting to all shell-mode buffers
;;(add-hook 'shell-mode-hook       
;;	  '(lambda ()         
;;	     (font-lock-mode t)       
;;	     (buffer-face-mode t) ; only in emacs 23         
;;	     (buffer-face-set 'font-lock-small-face)          ;; other stuff    
;;	     ))
;;end of set face


;;(set-face-foreground 'modeline "red") ;;

;;将状态栏中的字体设为红色

;;自定义math-lang-mode
;;(setq myKeywords 
;; '(("Sin\\|Cos\\|Sum" . font-lock-function-name-fac


;;   ("Pi\\|Infinity" . font-lock-constant-face)
;;  )
;;)
;;
;;(define-derived-mode math-lang-mode fundamental-mode
;;  (setq font-lock-defaults '(myKeywords))
;;  (setq mode-name "math lang")
;;)
;;math-lang-mode 结束

;;连接列表
;;(setq xlist '(1 2))
;;(setq ylist '(3 4))
;;(setq xylist `(,@xlist ,@ylist))



;;run-current-file 函数能根据buffer中的文件后缀名选择程序运行它
(defun run-current-file () 
  "Execute or compile the current file.
For example, if the current buffer is the file x.pl,
then it'll call “perl x.pl” in a shell.
The file can be php, perl, python, ruby, javascript, bash, ocaml, java.
File suffix is used to determine what program to run."
  (interactive)
  (let (extention-alist fname suffix progName cmdStr)
    (setq extention-alist ; a keyed list of file suffix to comand-line program to run
          '(
            ("php" . "php")
            ("pl" . "perl")
            ("py" . "python")
            ("rb" . "ruby")
            ("js" . "js")
            ("sh" . "bash")
            ("ml" . "ocaml")
            ("vbs" . "cscript")
            ("java" . "javac")
	    ("c"    .  "gcc")
            )
          )
    (setq fname (buffer-file-name))
    (setq suffix (file-name-extension fname))
    (setq progName (cdr (assoc suffix extention-alist)));;(assoc suffix ..)=> ("php" . "php")
    (setq cmdStr (concat progName " \""   fname "\""))
    (save-buffer fname)
    (if (string-equal suffix "el")
        (load-file fname)      (if progName                    ; is not nil
          (progn
            (message "Running...")
            (shell-command cmdStr))
        (message "No recognized program file suffix for this file.")
        ) )))

;;end of run-current-file

;;set flyspell-mode program as aspell
(setq ispell-program-name "D:\\gnu\\Aspell\\bin\\aspell.exe")
(setq-default ispell-local-dictionary "american")

;;end of set flyspell-mode


;;add mingw to path
;;(setenv "PATH" (concat  (getenv "PATH") ";D:\\gnu\\mingw\\msys\\2.0\\bin\\");;没有设置成功，？
;;(setq exec-path (append exec-path '("D:/gnu/mingw/msys/2.0/bin")))
;;end of adding to path

;;set where msys was install
(setq w32shell-msys-bin "D:\\gnu\\mingw\\msys\\2.0\\bin\\")

;;关闭错误提示声
(setq visible-bell t)

(setq mouse-yank-at-point t)
;;打开递归编辑
(setq enable-recursive-minibuffers t)

;;当鼠标靠近前三行时，就开始滚动
;; (setq scroll-margin 3
;;       scroll-conservatively 10000)
;;括号匹配时显示另外一边的括号，而不是烦人的跳到另一个括号
(show-paren-mode t)
(setq show-paren-style 'parentheses)
;;鼠标不要挡住光标
;; (mouse-avoidance-mode 'animate)
;;打开图像模式
;;(auto-image-file-mode)
;;进行语法高亮
(global-font-lock-mode 1)
;;设置dired可以递归copy and delete files
(setq dired-recursive-copies 'top)
(setq dired-recursive-deletes 'top)

;;鼠标滚轮平滑滚动
(defun smooth-scroll (increment)
  (scroll-up increment) (sit-for 0.05)
  (scroll-up increment) (sit-for 0.02)
  (scroll-up increment) (sit-for 0.02)
  (scroll-up increment) (sit-for 0.05)
  (scroll-up increment) (sit-for 0.05)
  (scroll-up increment) (sit-for 0.02)
  (scroll-up increment) (sit-for 0.02)
  (scroll-up increment) (sit-for 0.05)
  (scroll-up increment) (sit-for 0.06)
  (scroll-up increment))



;;end of 滚轮
;;将del键设为kill-region


(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(canlock-password "a1167c98163e7483ff3e65330157e3850f263d5c")
 '(column-number-mode t)
 '(cperl-under-as-char t)
 '(display-time-mode t)
 '(ecb-options-version "2.40")
 '(ecb-primary-secondary-mouse-buttons (quote mouse-1--mouse-2))
 '(ecb-tip-of-the-day nil)
 '(matlab-indent-level 6)
 '(mouse-1-click-in-non-selected-windows t)
 '(mouse-drag-copy-region nil)
 '(transient-mark-mode t)
 '(w32shell-cygwin-bin "D:\\gnu\\cygwin\\bin"))

;;;;; ------ Octave-mode ---------------------------------------------
    ;; a tip from Jason Mobarak, http://cs.unm.edu/~jmob/windows-octave.txt
    ;; I don't know the reason
       (setenv "DISPLAY" "127.0.0.1:0.0")    ;; autolad octave mode for *.m-files
       (autoload 'octave-mode "octave-mod" nil t)
       (setq auto-mode-alist
       (cons '("\\.m$" . octave-mode) auto-mode-alist))    ;; to turn on the abbrevs, auto-fill and font-lock features automatically  
       (add-hook 'octave-mode-hook
         (lambda ()
         (abbrev-mode 1)
         (auto-fill-mode 1)
         (if (eq window-system 'x)
          (font-lock-mode 1))))    ;; And finally, inferior-octave-mode-hook is run after starting the process 
    ;; and putting its buffer into Inferior Octave mode. Hence, if you like 
    ;; the up and down arrow keys to behave in the interaction buffer as in 
    ;; the shell, and you want this buffer to use nice colors:
    
       (add-hook 'inferior-octave-mode-hook
         (lambda ()
           (turn-on-font-lock)
           (define-key inferior-octave-mode-map [up]
             'comint-previous-input)
           (define-key inferior-octave-mode-map [down]
             'comint-next-input)))     ;; RET adds a newline and indents it 
;;(add-hook 'inferior-octave-mode-hook
;;	  '(lambda ()
;;	     (setq inferior-octave-program
;;		   "octave")))
       (add-hook 'octave-mode-hook 
         '(lambda nil
           (define-key octave-mode-map "\C-m"
              'octave-reindent-then-newline-and-indent)
           (define-key octave-mode-map [(control space)]
              'octave-complete-symbol) ;; I prefere C-SPC
           (define-key octave-mode-map [(meta control tab)] 
              'octave-complete-symbol)))    ;; run an inferior Octave process in a special Emacs buffer
       (autoload 'run-octave "octave-inf" nil t)    ;; if you have the win32 version of octave
       
   ;; If `gnuserv' is installed, add the lines
    ;;   (autoload 'octave-help "octave-hlp" nil t)
      (require 'gnuserv)
       (gnuserv-start)   

;;配置haskell-mode
(load "haskell-site-file.el")
(setq haskell-program-name "D:\\gnu\\ghc-6.12.2\\bin\\ghci.exe")
(add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
(add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)

;;end of setting haskell-mode



;;设置mew
;;(setq load-path (cons "/usr/local/share/emacs/site-lisp/mew" load-path))
(autoload 'mew "mew" nil t)
(autoload 'mew-send "mew" nil t)
;;(setq mew-icon-directory "/usr/local/share/emacs/site-lisp/mew/etc")
(setq mew-use-cached-passwd t)
(if (boundp 'read-mail-command)
    (setq read-mail-command 'mew))
(autoload 'mew-user-agent-compose "mew" nil t)
(if (boundp 'mail-user-agent)
    (setq mail-user-agent 'mew-user-agent))
(if (fboundp 'define-mail-user-agent)
    (define-mail-user-agent
       'mew-user-agent
       'mew-user-agent-compose
       'mew-draft-send-message
       'mew-draft-kill
       'mew-send-hook))
(setq mew-pop-size 0)
(setq mew-smtp-auth-list nil)
(setq toolbar-mail-reader 'Mew)
(set-default 'mew-decode-quoted 't)  
(when (boundp 'utf-translate-cjk)
      (setq utf-translate-cjk t)
      (custom-set-variables
         '(utf-translate-cjk t)))
(if (fboundp 'utf-translate-cjk-mode)
    (utf-translate-cjk-mode 1)) 

;(setq w3-configuration-directory "D:\\gnu\\Emacs23.2\\site-lisp\\w3-4.0pre.47\\")
;(setq load-path (cons "e:/emacs/site-listp/w3-4.0pre.47/lisp" load-path))
;(require 'w3)

;;将cygwin设为默认的shell

;; (require 'cygwin-mount)
;; (setenv "PATH" (concat "d:/gnu/cygwin/bin;" (getenv "PATH"))) 
;; (setq exec-path (cons "d:/gnu/cygwin/bin/" exec-path)) 
;; (cygwin-mount-activate)

;; (add-hook 'comint-output-filter-functions 
;;    'shell-strip-ctrl-m nil t) 
;; (add-hook 'comint-output-filter-functions 
;;    'comint-watch-for-password-prompt nil t)

(setq explicit-shell-file-name "cmdproxy.exe") 
;; For subprocesses invoked via the shell 
;; (e.g., "shell -c command") 
(setq shell-file-name explicit-shell-file-name)
 
;;end of cygwin


;;w3m

;; (require 'w3m-load)
;; ;;(require 'w3m-load)
;; (setq w3m-use-favicon nil)
;; (setq w3m-command-arguments '("-cookie" "-F"))
;; (setq w3m-use-cookies t)
;; (setq w3m-home-page "http://www.google.com")

;; (setq browse-url-browser-function 'w3m-browse-url)
;; (autoload 'w3m-browse-url "w3m" "Ask a WWW browser to show a URL." t)

;;配置slime
;;(add-to-list 'load-path "~/hacking/lisp/slime/")  ; your SLIME directory
;;(setq inferior-lisp-program "sbcl --noinform") ; your Lisp system
(setq inferior-lisp-program "clisp")
(require 'slime)
(slime-setup)

;;end of slime


;; 实现shell中的clear命令
(add-hook 'shell-mode-hook 'n-shell-mode-hook)
(defun n-shell-mode-hook ()
  "12Jan2002 - sailor, shell mode customizations."
  (local-set-key '[up] 'comint-previous-input)
  (local-set-key '[down] 'comint-next-input)
  (local-set-key '[(shift tab)] 'comint-next-matching-input-from-input)
  (setq comint-input-sender 'n-shell-simple-send)
  )

(defun n-shell-simple-send (proc command)
  "17Jan02 - sailor. Various commands pre-processing before sending to shell."
  (cond
   ;; Checking for clear command and execute it.
   ((string-match "^[ \t]*clear[ \t]*$" command)
    (comint-send-string proc "\n")
    (erase-buffer)
    )
   ;; Checking for man command and execute it.
   ((string-match "^[ \t]*man[ \t]*" command)
    (comint-send-string proc "\n")
    (setq command (replace-regexp-in-string "^[ \t]*man[ \t]*" "" command))
    (setq command (replace-regexp-in-string "[ \t]+$" "" command))
    ;;(message (format "command %s command" command))
    (funcall 'man command)
    )
   ;; Send other commands to the default handler.
   (t (comint-simple-send proc command))
   )
  )

;;end of clear

;; emacs中支持ｆｔｐ
(defun ftp (host)
  "03Mar01, sailor"
  "Run the ftp program using cygwin ftp."
  "Fixed the problem that the login prompt cannot be seen."
  (interactive "sFtp to Host :" )
  (let ((bufname)
        (bufobject))
    (setq bufname (concat "*ftp-" host "*"))
    (setq bufobject (get-buffer bufname))

    (cond
     ((and bufobject (get-buffer-process bufobject))
      (pop-to-buffer bufname)
      )
     (t
      (let ((login)
            (process)
            (ftp-program "ftp.exe"))
        (setq bufobject (get-buffer-create bufname))
        (pop-to-buffer bufobject)
        (comint-mode)
        (setq login (read-from-minibuffer (format "%s - Login : " host)))
        (comint-exec bufobject bufname ftp-program nil
            (list "--prompt=ftp> " "-v" host))
        (message "Login in progress. Please wait ...")
        (send-invisible (format "%s" login))
        (setq process (get-buffer-process (current-buffer)))
        (accept-process-output process)
        )
      )
     )
    )
  )

;;end of ftp

;;run tc

(defun run-total-commander ()   
  (interactive)   
  (w32-shell-execute "open" "D:\\GreenSoftWare\\TotalCommander7.5\\totalcmd.exe")   
  )   

;;end of set tc

;;set telnet in emacs
(require 'telnet)

(defun telnet (host)
  "Open a network login connection to host named HOST (a string).
Communication with HOST is recorded in a buffer `*PROGRAM-HOST*'
where PROGRAM is the telnet program being used.  This program
is controlled by the contents of the global variable
`telnet-host-properties', falling back on the value of the
global variable `telnet-program'. Normally input is edited
in Emacs and sent a line at a time."
  (interactive "sOpen connection to host: ")
  (let* ((comint-delimiter-argument-list '(?\  ?\t))
         (properties (cdr (assoc host telnet-host-properties)))
         (telnet-program (if properties (car properties) telnet-program))
         (name (concat telnet-program "-" (comint-arguments host 0 nil) ))
         (buffer (get-buffer (concat "*" name "*")))
         (telnet-options (if (cdr properties)
                 (cons "-l" (cdr properties))))
         process)
    (if (and buffer (get-buffer-process buffer))
        (pop-to-buffer (concat "*" name "*"))
      (pop-to-buffer
       (apply 'make-comint name telnet-program nil telnet-options))
      (setq process (get-buffer-process (current-buffer)))
      ;;(set-process-filter process 'telnet-initial-filter)
      ;; Don't send the `open' cmd till telnet is ready for it.
      ;;(accept-process-output process)
      (erase-buffer)
      (send-string process (concat "open " host "\n"))
      (telnet-mode)
      (setq telnet-remote-echoes nil)
      (setq telnet-new-line "\n") ;; needed for cygwin 1.3.11
      (setq comint-input-sender 'telnet-simple-send)
      (setq telnet-count telnet-initial-count)
      (setq comint-process-echoes t)
      )))

;;end of telnet


;解决emacs shell 乱码
;(setq ansi-color-for-comint-mode t)
;(customize-group 'ansi-colors)
;(kill-this-buffer);关闭customize窗口

;; answer y or n to replace yes-no
(fset 'yes-or-no-p 'y-or-n-p)

;;配置auto-complete
;; (require 'auto-complete-config)
;; (ac-config-default)
;; (setq ac-auto-start nil)
;; (ac-set-trigger-key "TAB") 


;;输命令时给出可选的提示
(icomplete-mode 1)

;;显示行数
;;(linum-mode)

;;按%号使括号跳转

(defun match-paren (arg)
  "Go to the matching paren if on a paren; otherwise insert %."
  (interactive "p")
  (cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
	((looking-at "\\s\)") (forward-char 1) (backward-list 1))
	(t (self-insert-command (or arg 1)))))

;;注释区域与反注释区域

;;(comment-region)
;;(uncomment-region)


;; 将win键设置为修饰键
(when (eq system-type 'windows-nt)
  (setq w32-pass-lwindow-to-system nil
        w32-pass-rwindow-to-system nil
        w32-pass-apps-to-system nil
        w32-capslock-is-shiftlock nil
        w32-enable-caps-lock nil
        w32-lwindow-modifier 'super ;; Left Windows key
        w32-rwindow-modifier 'alt
        w32-recognize-altgr nil
        w32-apps-modifier 'hyper

        ))




;;类似于vim中的跳转。按C-=保存当前位置，C－-跳转到上次位置。


(defun ska-point-to-register()
  "Store cursorposition _fast_ in a register. Use ska-jump-to-register to jump back to the stored  position."
  (interactive)
  (setq zmacs-region-stays t)
  (point-to-register 8))

(defun ska-jump-to-register()
  "Switches between current cursorposition and position that was stored with ska-point-to-register."
  (interactive)
  (setq zmacs-region-stays t)
  (let ((tmp (point-marker)))
        (jump-to-register 8)
        (set-register 8 tmp)))


;;;;类似vim中的fx功能，当按c-c f时输入要查找的字符，按一次跳一次
(defun wy-go-to-char (n char)
  "Move forward to Nth occurence of CHAR.
Typing `wy-go-to-char-key' again will move forwad to the next Nth
occurence of CHAR."
  (interactive "p\ncGo to char: ")
  (search-forward (string char) nil nil n)
  (while (char-equal (read-char)
		     char)
    (search-forward (string char) nil nil n))
  (setq unread-command-events (list last-input-event)))

(defun wy-back-to-char (n char)
  "Move forward to Nth occurence of CHAR.
Typing `wy-back-to-char-key' again will move forwad to the next Nth
occurence of CHAR."
  (interactive "p\ncGo to char: ")
  (search-forward (string char) nil nil (- 0 n))
  (while (char-equal (read-char)
		     char)
    (search-forward (string char) nil nil (- 0 n)))
  (setq unread-command-events (list last-input-event)))

(define-key global-map (kbd "C-c b") 'wy-back-to-char)
(define-key global-map (kbd "C-c f") 'wy-go-to-char)

;; 启用以下功能
(put 'narrow-to-region 'disabled nil)
(put 'dired-find-alternate-file 'disabled nil)

;; 增加自定义关键字
;; (dolist (mode '(c-mode c++-mode java-mode lisp-mode emacs-lisp-mode lisp-interaction-mode sh-mode
;;                        sgml-mode))
;;   (font-lock-add-keywords mode
;;                           '(("\\<\\(FIXME\\|TODO\\|Todo\\|HACK\\):" 1 font-lock-warning-face prepend)
;;                             ("\\<\\(and\\|or\\|not\\)\\>" . font-lock-keyword-face)
;;                             ("(\\|)" . beautiful-blue-face)
;;                             ("\\[\\|]" . yellow-face)
;;                             ("<\\|>" . cyan-face)
;;                             ("{\\|}" . green-face))))



;;这个函数能根据括号来使代码对齐
(defun ywb-indent-accoding-to-paren ()
  "按块([]{}())来格式化代码"
  (interactive)
  (let ((prev-char (char-to-string (preceding-char)))
        (next-char (char-to-string (following-char)))
        (pos (point)))
    (save-excursion
      (cond ((string-match "[[{(<]" next-char)
	     (indent-region pos (progn (forward-sexp 1) (point)) nil))
            ((string-match "[\]})>]" prev-char)
             (indent-region (progn (backward-sexp 1) (point)) pos nil))))))


;; 没有提示音,也不闪屏

(setq ring-bell-function 'ignore)



				   
(defun xilbert-insert-previous-content ()
  (interactive)
  (if (> (length kill-ring-yank-pointer) 1)
      (insert (nth 1 kill-ring-yank-pointer))
    (message "kill-ring has no two items")))



(setq x-select-enable-clipboard nil)
(put 'narrow-to-page 'disabled nil)


;;800次按键后自动保存文件
(setq auto-save-interval 800)

;;回车既对齐页换行



;;set for eshell
(add-hook 'eshell-mode-hook
   '(lambda nil
   ;;(eshell/export "EPOCROOT=\\Paragon\\")
   (let ((path))
      (setq path ".;D:/gnu/Emacs23.2/EmacsW32/gnuwin32/bin/")
      ;;(setq path (concat path ";d:/program files/microsoft visual studio/vc98/bin"))
    (setenv "PATH" path))
 ;;  (local-set-key "\C-u" 'eshell-kill-input))
 ))

;;clear eshell

(defun eshell/clear ()
  "04Dec2001 - sailor, to clear the eshell buffer."
  (interactive)
  (let ((inhibit-read-only t))
    (erase-buffer)))

;;;; CC-mode配置  http://cc-mode.sourceforge.net/
(require 'cc-mode)
;;(c-set-offset 'inline-open 0)
;;(c-set-offset 'friend '-)
;;(c-set-offset 'substatement-open 0)

;;;;我的C/C++语言编辑策略

;; (defun my-c-mode-common-hook()
;;   (setq tab-width 4 indent-tabs-mode t)
;;   ;;; hungry-delete and auto-newline
;;   (c-toggle-auto-hungry-state 1)
;;   ;;按键定义
;;   (define-key c-mode-base-map [(control \`)] 'hs-toggle-hiding)
;;   (define-key c-mode-base-map [(return)] 'newline-and-indent)
;;  ;; (define-key c-mode-base-map [(f5)] 'compile)
;;   (define-key c-mode-base-map [(meta \`)] 'c-indent-command)
;; ;;  (define-key c-mode-base-map [(tab)] 'hippie-expand)
;;   (define-key c-mode-base-map [(tab)] 'my-indent-or-complete)
;;   (define-key c-mode-base-map [(meta ?/)] 'semantic-ia-complete-symbol-menu)
;;     ;;预处理设置
;;   (setq c-macro-shrink-window-flag t)
;;   (setq c-macro-preprocessor "cpp")
;;   (setq c-macro-cppflags " ")
;;   (setq c-macro-prompt-flag t)
;;   (setq hs-minor-mode t)
;;   (setq abbrev-mode t)
;; )

;; (add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

;;;;我的C++语言编辑策略
(defun my-c++-mode-hook()
  (setq tab-width 6 indent-tabs-mode nil)
  (c-set-style "stroustrup")
;;  (define-key c++-mode-map [f3] 'replace-regexp)
)



;;自动补齐

(defun my-indent-or-complete ()
   (interactive)
   (if (looking-at "\\>")
 	  (hippie-expand nil)
 	  (indent-for-tab-command))
 )

(define-key c-mode-base-map [(tab)] 'my-indent-or-complete)
;;set for hippie-expand
(setq hippie-expand-try-functions-list
 	  '(
		try-expand-dabbrev
                try-expand-dabbrev-visible
		try-expand-dabbrev-all-buffers
		try-expand-dabbrev-from-kill
		try-expand-list
		try-expand-list-all-buffers
		try-expand-line
        try-expand-line-all-buffers
        try-complete-file-name-partially
        try-complete-file-name
        try-expand-whole-kill
        )
)

(global-set-key (kbd "M-/") 'hippie-expand)


(setq auto-mode-alist (cons '("\\.cpp\\'" . c++-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.c\\'" . c-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.el\\'" . emacs-lisp-mode) auto-mode-alist))

;;end of setting c or c++ language
;;自动补全按键
;;Tab or M-/ or C-Tab

(require 'xcscope)



;;set for auctex

(load "auctex.el" nil t t)
(load "preview-latex.el" nil t t)
(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq-default TeX-master nil)
(add-hook 'LaTeX-mode-hook #'LaTeX-install-toolbar)
(add-hook 'LaTeX-mode-hook 'turn-on-auto-fill)
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)

;;maxima
;;(add-to-list 'load-path "~/site-lisp/maxima")
(add-to-list 'Info-default-directory-list "d:/Program Files/Maxima-5.21.0/info")
(autoload 'imaxima "imaxima" "Image support for Maxima." t)
(autoload 'maxima-mode "maxima" "Maxima mode" t)
(autoload 'maxima "maxima" "Maxima interaction" t)
(autoload 'imath-mode "imath" "Interactive Math minor mode" t)
(setq auto-mode-alist (cons '("\\.max" . maxima-mode) auto-mode-alist))

(setq imaxima-use-maxima-mode-flag t)
(setq maxima-use-dynamic-complete t)
(setq imaxima-fnt-size "Large")
(setq imaxima-scale-factor 1.0)
(setq imaxima-label-color "red")
(setq imaxima-equation-color "white")
(setq imaxima-max-scale 0.5)
(setq imaxima-linearize-flag t)
(setq imaxima-gs-program "gswin32c")

(defun wy-maxima-mode-hook ()
  (setq wy-starting-imaxima nil)
  (defun maxima-start ()
    "Start the Maxima process."
    (interactive)
    (if (not (processp inferior-maxima-process))
        (if (not wy-starting-imaxima)
            (let ((origbuffer (current-buffer)))            
              (setq wy-starting-imaxima t)
              (imaxima)
              (setq wy-starting-imaxima nil)
              (switch-to-buffer origbuffer))))
    (if (processp inferior-maxima-process)
        (unless (eq (process-status inferior-maxima-process) 'run)
          (delete-process inferior-maxima-process)
          (save-excursion
            (set-buffer "*maxima*")
            (erase-buffer))
          (setq inferior-maxima-process nil)))
    (unless (processp inferior-maxima-process)
      (setq maxima-input-end 0)
      (let ((mbuf)
            (cmd))
        (if maxima-args
            (setq cmd 
                  (append (list 'make-comint "maxima" maxima-command
                                nil) (split-string maxima-args))) 
          (setq cmd (list 'make-comint "maxima" maxima-command)))
        (setq mbuf (eval cmd))
        (save-excursion
          (set-buffer mbuf)
          (setq inferior-maxima-process (get-buffer-process mbuf))
          (if maxima-fix-double-prompt
              (add-to-list 'comint-output-filter-functions
                           'maxima-remove-double-prompt))
          (accept-process-output inferior-maxima-process)
          (while (not (maxima-new-prompt-p))
            (accept-process-output inferior-maxima-process))
          (inferior-maxima-mode)))
      (sit-for 0 maxima-after-output-wait))))

(add-hook 'maxima-mode-hook 'wy-maxima-mode-hook)
(setq maxima-command "D:/Program Files/Maxima-5.21.0/bin/maxima.bat")
(defun run-maxima ()
  (interactive)
  (pop-to-buffer "*maxima*")
  ;;(maxima-start)
  (maxima))

;;use ahei's defined face
(require 'util)
(require 'ahei-face)
(require 'color-theme-ahei)
;;(color-theme-ahei)
;; (font-lock-add-keywords 'emacs-lisp-mode
;; 			'(
;; 			  ("\\*.*$" . yellow-face)
;; 			  ))


;;hightlight symbol in this file

(require 'highlight-symbol)
;;modify outline-mode-map-prefix
;;经过此番修改，outline-mode与outline-minor-mode的按键一致

(add-hook 'outline-mode-hook
	  '(lambda ()
             (let ((map (make-sparse-keymap)))
               (define-key map "\C-o" outline-mode-prefix-map)
               (define-key map [menu-bar] outline-mode-menu-bar-map)
               (setq outline-mode-map map)
               )))


;; 使得标题高亮显示
(add-hook 'outline-minor-mode-hook
	  '(lambda ()

	     (hi-lock-set-pattern "^\\*[^*]*?$" 'cyan-face)
	     (hi-lock-set-pattern "^\\*\\*[^*]*?$" 'yellow-face)
	     (hi-lock-set-pattern "^\\*\\*\\*[^*]*?$" 'magenta-face)))

;; M-f1 打开outline-minor-mode


;;设置行距
(setq-default line-spacing 4)


(autoload 'ibuffer "ibuffer" "List buffers." t)

;;插入utf8字符
;;(ucs-insert 174) ®
;;(ucs-insert 969) ω

;;新建C-c r 热键前缀，将browse-kill-ring 绑定在C-c r k
(require 'browse-kill-ring)
(define-prefix-command 'ctrl-c-r-map)




;; ido ,used as helper when switch buffer or find a file
(require 'ido)
(ido-mode t)
(setq ido-enable-flex-matching t) ;; enable fuzzy matching
;;(setq ido-save-directory-list-file t)

;;自定义C-x x 热键前缀
(define-prefix-command 'ctrl-x-x-map)
(define-prefix-command 'ctrl-c-c-map)


;;令按键迅速回显
(setq echo-keystrokes -1)

;;
;; (add-hook 'mouse-leave-buffer-hook
;; 	  '(lambda ()
;; 	     (message "鼠标离开选择区域")))




;;c-return 执行当前sexp

;;M-f5 运行shell命令

;;M-f2 显示行


;;python mode
(add-hook 'python-mode-hook
          (lambda ()
            (set (make-variable-buffer-local 'beginning-of-defun-function)
                 'py-beginning-of-def-or-class)
            (set-variable 'py-indent-offset 4)
            ;; (smart-operator-mode-on)
            (setq outline-regexp "def\\|class ")))

;;用空格缩进
(setq-default indent-tabs-mode nil)

;;how to customlize imenu-tree?

;;(require 'imenu-tree)


;;设置hide region
(require 'hide-region)

(setq hide-region-before-string  "@[------------------------")
(setq  hide-region-after-string  "区域已隐藏----------------------]@")




;;toggle current buffer read-only state


;;when code for emacs subdirectory ,toggle read only on 
(add-hook 'emacs-lisp-mode-hook '(lambda ()
                                   (interactive)
                                   (if (and
                                        (stringp (buffer-file-name))
                                        (string-match-p "d:/gnu/Emacs[0-9.].*" (buffer-file-name)))
                                       (toggle-read-only)
                                     )))


;;replace string in string
;;(replace-regexp-in-string "\\(hello\\) \\(world\\)" "\\2 \\1" "hello world")

;;设置compile
(defun change-to-cygwin-style-path (emacs-style-path-on-w32)
  (let ((full-path emacs-style-path-on-w32))
    (replace-regexp-in-string "\\(\\w+?\\):\\(\\w*\\)" "/cygdrive/\\1\\2" full-path)))


(defvar my-compile-hook nil)

;;这是主函数，按f5执行此函数
(defun my-compile-main-fun ()
  (interactive)
  (run-hooks  'my-compile-hook))

;;这个函数用来编译现在用得到的语言写的代码，以后如果要用到其他语言，可以写个函数，然后加入到my-compile-hook中去。
(defun orginal-compile-fun ()
  (let ((mode major-mode)
	(compstr nil))
    (cond ((eq mode 'c-mode)
	   (setq compstr (concat "gcc -std=\"c99\" " (change-to-cygwin-style-path (buffer-file-name)))))
	  ((eq mode 'c++-mode)
	   (setq compstr (concat "c++  " (change-to-cygwin-style-path (buffer-file-name)))))
	  ((eq mode 'emacs-lisp-mode)
	   (emacs-lisp-byte-compile))
	  ((eq mode 'python-mode)
	   (setq compstr (concat "python " (buffer-file-name)))
           )
	  ((or (eq mode 'cperl-mode) (eq mode 'perl-mode))
	   (setq compstr (concat "D:/gnu/openperl/perl/bin/perl.exe " (buffer-file-name))))
	  )
    (save-buffer)
    (if compstr 
	(compile compstr))))
;;将orginal-compile-fun加入my-compile-hook中
(add-hook 'my-compile-hook 'orginal-compile-fun)

;;将my-compile-main-fun 绑定到f5上


;;octave-mode compile
(add-hook 'my-compile-hook '(lambda ()
                              (if (or (eq major-mode 'octave-mode) (eq major-mode 'matlab-mode))
                                  (progn
                                    (save-buffer)
                                    (compile (concat "octave --silent " (buffer-file-name)))))))


;;(setq my-compile-hook nil)

;;markdown-mode 
(autoload 'markdown-mode "markdown-mode.el"
  "Major mode for editing Markdown files" t)
(setq auto-mode-alist
      (cons '("\\.text"  .markdown-mode) auto-mode-alist))

;;set for minibuffer
;;(setq minibuffer-frame-alist nil)
;; (setq minibuffer-frame-alist
;;       '((top . 80) (left . 10) (width . 80) (height . 20)))

;;stardic
;; author: pluskid

(defun kid-star-dict ()
  (interactive)
  (let ((begin (point-min))
        (end (point-max)))
    (if mark-active
        (setq begin (region-beginning)
              end (region-end))
      (save-excursion
        (backward-word)
        (mark-word)
        (setq begin (region-beginning)
              end (region-end))))
    ;; 有时候 stardict 会很慢，所以在回显区显示一点东西
    ;; 以免觉得 Emacs 在干什么其他奇怪的事情。
    (message "searching for %s ..." (buffer-substring begin end))
    (tooltip-show
     (shell-command-to-string
      (concat "sdcv -n "
              (buffer-substring begin end))))))


;; 调用 stardict 的命令行程序 sdcv 来查辞典
;; 如果选中了 region 就查询 region 的内容，否则查询当前光标所在的单词
;; 查询结果在一个叫做 *sdcv* 的 buffer 里面显示出来，在这个 buffer 里面
;; 按 q 可以把这个 buffer 放到 buffer 列表末尾，按 d 可以查询单词

(defun kid-sdcv-to-buffer ()
  (interactive)
  (let ((word (if mark-active
                  (buffer-substring-no-properties (region-beginning) (region-end))
                (current-word nil t))))
    (setq word (read-string (format "Search the dictionary for (default %s): " word)
                            nil nil word))
    (set-buffer (get-buffer-create "*sdcv*"))
    (buffer-disable-undo)
    (erase-buffer)
    (let ((process (start-process-shell-command "sdcv" "*sdcv*" "sdcv" "-n" word)))
      (set-process-sentinel
       process
       (lambda (process signal)
         (when (memq (process-status process) '(exit signal))
           (unless (string= (buffer-name) "*sdcv*")
             (setq kid-sdcv-window-configuration (current-window-configuration))
             (switch-to-buffer-other-window "*sdcv*")
             (local-set-key (kbd "d") 'kid-sdcv-to-buffer)
             (local-set-key (kbd "q") (lambda ()
                                        (interactive)
                                        (bury-buffer)
                                        (unless (null (cdr (window-list))) ; only one window
                                          (delete-window)))))
           (goto-char (point-min))))))))


;; org-mode setting
(setq org-todo-keywords
      '((sequence "TODO(t)" "DOING(i!)" "HANGUP(h!)" "|" "DONE(d!)" "CANCEL(c!)")))

;;emaci is a mode for viewing file ,its' author is ahei
;;(require 'emaci)

(defun isearch-cur-word (fun)
  "ISearch current word use function FUN."
  (let ((cur-word (current-word)))
    (if (not cur-word)
        (message "No word under cursor.")
      (call-interactively fun)
      (isearch-yank-string cur-word))))

(defun isearch-forward-cur-word (&optional backward)
  "`isearch-forward' current word."
  (interactive "P")
  (let ((fun (if backward 'isearch-backward 'isearch-forward)))
    (isearch-cur-word fun)))


;; set c-' to isearch current word

;;set for org
(setq org-publish-project-alist
      '(("orgfiles"
         :base-directory "d:/廖小卿/文献与文档/笔记/"
         :base-extension "org"
         :publishing-directory "d:/廖小卿/文献与文档/笔记/org/html/"
         :publishing-function org-publish-org-to-html
         :exclude "sandbox.org"   ;; regexp
         :headline-levels 3
         :section-numbers nil
         :table-of-contents nil
         :style "<link rel=\"stylesheet\"
                      href=\"d:/廖小卿/文献与文档/笔记/org/html/other/stylesheet.css\" type=\"text/css\"/>"
         :auto-preamble t
         :auto-postamble nil)

        ("images"
         :base-directory "d:/廖小卿/文献与文档/笔记/org/images/"
         :base-extension "jpg\\|gif\\|png"
         :publishing-directory "d:/廖小卿/文献与文档/笔记/org/html/images/"
         :publishing-function org-publish-attachment)
        ("other"
         :base-directory "d:/廖小卿/文献与文档/笔记/"
         :base-extension "css\\|el"
         :publishing-directory "d:/廖小卿/文献与文档/笔记/org/html/other/"
         :publishing-function org-publish-attachment)
        ("website" :components ("orgfiles" "images" "other"))))

;;(setq org-archive-location "E:/文献与文档/笔记")
(add-hook 'emacs-lisp-hook (function cscope:hook))

(setq org-hide-leading-stars t)
;; (add-hook 'org-mode-map '(lambda
;;                            (interactive)
;;                            (setq auto-complete-mode t)))


;; turn on soft wrapping mode for org mode

(add-hook 'org-mode-hook
          (lambda () (setq truncate-lines nil)))

(add-hook 'org-mode-hook '(lambda ()
                            (define-key org-mode-map (kbd "<M-return>") 'org-insert-subheading)
                            (define-key org-mode-map (kbd "<C-return>") 'org-insert-heading)
                            (define-key org-mode-map (kbd "C-c C-g") 'outline-forward-same-level)
                            ))

;;set for scroll-bar

;;set for muse
;;using muse-mode
(require 'muse-mode) ; load authoring mode
(require 'muse-html) ; load publishing styles I use
(require 'muse-project) ; publish files in projects
(setq muse-project-alist
      '(("mymuse" ("~/mymuse" :default "index")
         (:base "html" :path "~/public_html/mymuse"))))

;;add menu for muse
(easy-menu-define muse-mode-menu muse-mode-map
  "menu for muse"
  `("muse"
    ["muse-index" muse-index]
    ["muse-find-backlinks(pages)" muse-find-backlinks]
    ["add a tag"  muse-insert-tag]
    ["font lock toggle" font-lock-mode]
    "---"
    ["publish-this-file" muse-publish-this-file]
    ["publish-project" muse-project-publish]
    ["muse-browser-result" muse-browse-result]
    ["increase-list-item-ind" muse-increase-list-item-indentation]
    ["decrease-list-item-ind"    muse-decrease-list-item-indentation]
    ))
;;set style sheet for muse
(setq muse-html-style-sheet  "<link rel=\"stylesheet\" href=\"../../org/css/docbook.css\" type=\"text/css\"/>")        
(setq muse-xhtml-style-sheet  "<link rel=\"stylesheet\" href=\"../../org/css/docbook.css\" type=\"text/css\"/>")
;;insert tag
(add-hook 'muse-mode-hook '(lambda ()
(define-key muse-mode-map (kbd "C-c C-t") 'muse-insert-tag)
))

(delete-selection-mode 1) ; turn on behavior that delete or type-over selected text


(defun xilbert-delete-current-word ()
  (interactive)
 (let (beg end ls)
    (setq  ls (bounds-of-thing-at-point 'word))
    (setq beg (car ls))
    (setq end (cdr ls))
    (delete-region beg end)))

;;set c-M-d to xilbert-delete-current-word
(defun xilbert-delete-to-line-beginning ()
  (interactive)
  (let (beg end)
    (save-excursion
      (setq beg (point))
      (beginning-of-line)
      (setq end (point))
      (delete-region beg end))))
;;set c-backspace to delete-to-line-beginning



;; delete text begin () [] or "" and <>
(defun delete-enclosed-text ()
  "Delete texts between any pair of delimiters."
  (interactive)
  (save-excursion
    (let (p1 p2)
      (skip-chars-backward "^(<[\“") (setq p1 (point))
      (skip-chars-forward "^)>]\”") (setq p2 (point))
      (delete-region p1 p2))))


(defun xilbert-get-char (pt)
      (buffer-substring-no-properties pt (1+ pt)))

(defun xilbert-delete-block ()
  (interactive)
  (let
      (
       (left 0)
        (right 0)
       (mypos 0)
       (lchar "")
       (rchar "")
       (ch "")
       (lstack nil)
       (rstack nil)
       (issearch 1)
       (mytable  '(("[" . "]")
                   ("]" . "[")
                   ("(" . ")")
                   (")" . "(")
                   ("{" . "}")
                   ("}" . "{")
                   ("<" . ">")
                   (">" . "<")))
       (my-search-str "\\[\\|\\]\\|(\\|)\\|<\\|>\\|{\\|}")
       error-code1 ;; 1 repenset error happen befor ,2 is opposite , 0 is ok
       error-code2
       (lcaps '("[" "<" "(" "{"))
       (rcaps '("]" ">" ")" "}"))
       )
    
    (save-excursion
      (setq error-code1 (block  outter-block
                          (setq mypos (point))
                          (while issearch
                            (setq issearch
                                  (re-search-backward my-search-str (point-min) t))
                            (setq ch (xilbert-get-char (point)))
                            (if (member ch lcaps)
                                (progn
                                  (cond ((> (length lstack) 0)
                                         (let (key value item)
                                           (setq item (assoc  ch mytable))
                                           (setq value (cdr item))
                                           (if (equal value (car lstack)) ;; if matches ,pop lstack
                                               (pop lstack)
                                             (return-from outter-block 1)))) ;;return error code
                                        (t (push ch lstack))) ;;if lstack is empty ,just push lstack
                                  )
                              (push ch lstack)) ;;parse character is done
                            (when (and (equal (length lstack) 1) (member (car lstack) 
lcaps))
                              (setq left (point))
                              (setq lchar ch)
                              (return-from outter-block 0))  ;;left search  successfully!
                            );;end of left search
                          (if (equal issearch nil)
                              (return-from outter-block 1))))
      (setq error-code2 (block outter-block
                          (goto-char mypos)
                          (while issearch
                            (setq issearch
                                  (re-search-forward my-search-str (point-max) t))
                            (setq ch (xilbert-get-char (1- (point))))
                            (if (member ch rcaps)
                                (progn
                                  (cond ((> (length rstack ) 0)
                                         (let (key value item)
                                           (setq item (assoc ch mytable))
                                           (setq value (cdr item))
                                           (if (equal value (car rstack))
                                               (pop rstack)
                                             (return-from outter-block 2)))) ;;return 2 error code
                                        (t (push ch rstack))))
                              (push ch rstack)
                              )
                            (when (and (equal (length rstack) 1) (member (car rstack) 
rcaps))
                              (setq right (point))
                              (setq rchar ch)
                              (return-from outter-block 0) ;;succefully
                              )) ;;end of search right
                          (when (not (equal lchar rchar))
                            (return-from outter-block 3))  ;;two side not mathced
                          )
            )
      (if (and (equal error-code1 0) (equal error-code2 0) (equal
                                                            (cdr (assoc lchar mytable)) rchar))
          (delete-region left right))
      )

    )
  )

;;set xilbert-delete-block to s-o

;;set  key for kill-ring


;;descibe-fun
(defun xilbert-descibe-fun-at-point ()
  "describe function at point"
  (interactive)
  (let (word)
    (setq word (current-word))
    (describe-function (intern-soft word))))



(defun xilbert-run-fun-at-point ()
  (interactive)
  (let (word)
    (setq word (current-word))
    (if (commandp (intern-soft word))
      (call-interactively
       (intern-soft word))
      (funcall (intern-soft word))
      )))


(defun run-shell-at-buffer ()
       (interactive)
       (let (command-string)
         (save-excursion
           (setq command-string  (buffer-substring-no-properties (region-beginning) (region-end) ))
           (goto-char (region-end))
           (newline)
        
           (shell-command command-string (quote (4)) command-string)
           )))

;;set for semantic

;; (require 'cedet)
;; (semantic-load-enable-code-helpers)

;;end semantic

;;set for auto-complete
;(setq emacs-lisp-mode-hook (cdr emacs-lisp-mode-hook))
;;auto-complete configure
;; (require 'auto-complete-config)
;; (ac-config-default) 
;; (define-key ac-mode-map (kbd "M-TAB") 'auto-complete) 

(require 'auto-complete)
(require 'auto-complete-config)
(setq ac-auto-start nil)

;;所有打开的buffer都打开auto-complete-mode
;; dirty fix for having AC everywhere
;; (define-globalized-minor-mode real-global-auto-complete-mode
;;   auto-complete-mode (lambda ()
;;                        (if (not (minibufferp (current-buffer)))
;;                            (auto-complete-mode 1))
;;                        ))
;; (real-global-auto-complete-mode t)
;;(ac-set-trigger-key "C-/")

(define-key ac-mode-map (kbd "M-/") 'auto-complete)
(apply-define-key
 ac-complete-mode-map
 `(("<return>"   ac-complete)
   ("RET"        nil)
   ("C-j"        ac-complete)
   ("<C-return>" ac-complete)
   ("C-n"        ac-next)
   ("C-p"        ac-previous)))
;;do what i mean
(setq ac-dwim t)

(set-default 'ac-sources
             '(;;ac-source-semantic
               ;;ac-source-yasnippet
               ;;ac-source-abbrev
               ac-source-words-in-buffer
               ac-source-words-in-all-buffer
               ;;ac-source-imenu
               ac-source-files-in-current-dir
               ac-source-filename))

;;use msf-abbrev mode

;; (require 'msf-abbrev)
;; ;; ensure abbrev mode is always ona
;; (setq-default abbrev-mode t)
;; ;; do not bug me about saving my abbreviations
;; (setq save-abbrevs nil)
;; (setq msf-abbrev-verbose t) ;; optional
;; (setq msf-abbrev-root "~/.emacs.d/mode-abbrevs")


;; (msf-abbrev-load)

;;在移动一个word的时候跳过连字符
(add-hook 'emacs-lisp-mode-hook '(lambda ()
                              (interactive)
                              (modify-syntax-entry ?- "w")))


;;set join-line c-\



;;删除光标周围的所有空行

;;删除光标周围的space，仅保留一个


;;插入一个块
(defun xilbert-insert-block (fn)
  "insert a emacs-lisp block"
  (interactive "Mplease input function:")
  (let ( (orgin-begin (region-beginning))
         (orgin-end (region-end)))
      (goto-char orgin-begin)
      (insert (concat "(" fn ))
      (goto-char (setq orgin-end (+ orgin-end (length fn) 1)))
      (insert ")")
      (goto-char orgin-begin)
      (call-interactively 'set-mark-command)
      (goto-char (+ orgin-end 1))
      (call-interactively 'indent-region)))



;;insert time stamp
(defun insert-time ()
(interactive)
(insert (concat "[[" (format-time-string "%Y-%m-%d-%R") "]]")))


(defun xilbert-insert-block2 ()
  (interactive)
  (insert "[]")
  (backward-char))

;;keyboard macro manage ;;bind to C-x M-e
(defun xilbert-kmacro-manage (arg)
  (interactive (list
                (string-to-number
                 (read-string "[(f:-1)&(b:1)&(e:0)&(2:i)&(3:k)&(4:n)]:" nil 'xilbert-history))))
  (let (cmd)
    (cond ((equal arg -1)
           (setq cmd 'kmacro-cycle-ring-previous))
          ((equal arg 1)
           (setq cmd 'kmacro-cycle-ring-next))
          ((equal arg 0)
           (setq cmd 'kmacro-edit-macro))
          ((equal arg 2)
           (setq cmd 'insert-kbd-macro))
          ((equal arg 3)
           (setq cmd 'kmacro-bind-to-key))
          ((equal arg 4)
           (setq cmd 'kmacro-bind-to-key)))
    (call-interactively cmd)
    )
  )


;;delete "block" or 'block'
(defun xilbert-delete-quote ()
  (interactive)
  (let (left right)
    (save-excursion
      (re-search-backward "\"\\|'")
      (forward-char)
      (setq left
            (point))
      (re-search-forward "\"\\|'")
      (setq right (- (point) 1))
      (delete-region left right))))

;;this function is used to search region content via browsers
;;(setq browse-url-browser-function 'browse-url-generic browse-url-generic-program "/usr/bin/firefox")
;;can select a browser you like

(defun google-region (beg end)
  "Google the selected region."
  (interactive "r")
  (browse-url
   (concat "http://www.google.com/search?ie=utf-8&oe=utf-8&q="
           (buffer-substring beg end))))

(defun perldoc-current-word (word)
  "Google the selected region."
  (interactive (list
                (current-word)))
  (browse-url
   (concat "http://perldoc.perl.org/search.html?q="
           word )))



;;set global key

(defun  xilbert-global-map-mode-kbd (key-hook-alist)
  (let (item hook key)
    (while key-hook-alist
      (setq item (car key-hook-alist))
      (setq key (car item))
      (setq hook (cdr item))
      (eval `(global-set-key ,key ',hook  ))
      (setq key-hook-alist (cdr key-hook-alist))
  )))


;;xilbert-map-mode-kbd set global-key for command
(xilbert-global-map-mode-kbd
 '(
   ("\C-x\C-m"  . execute-extended-command)
   ("\C-w" . backward-kill-word)
   ("\C-x\C-k" . kill-region)
   ((kbd "C-+") . text-scale-increase)
   ((kbd "") . ispell-complete-word)
   ([(mouse-5)] . (lambda () (interactive) (smooth-scroll 1)))
   ([mouse-4] . (lambda () (interactive) (smooth-scroll -1)))
   ([delete] . kill-region)
   ("\C-xm" . browse-url-at-point)
   ([f12] . run-total-commander)
   ("%" . match-paren)
   ((kbd "s-k") . uncomment-region)
   ((kbd "s-h") . comment-region)
   ([(control ?\=)] . ska-point-to-register)
   ([(control ?\-)] . ska-jump-to-register)
   ((kbd "C-M-]") . ywb-indent-accoding-to-paren)
   ((kbd "C-l") .   recenter-top-bottom )
   ((kbd "M-y") . xilbert-insert-previous-content)
   ("\C-m" . newline-and-indent)
   ([(control tab)] . my-indent-or-complete)
   ([(f10)] . speedbar)
   ([(control f3)] . highlight-symbol-at-point)
   ;;([f3] . highlight-symbol-next)
   ([(shift f3)] . highlight-symbol-prev)
   ([(meta f3)] . highlight-symbol-prev)
   ((quote [M-f1]) . outline-minor-mode)
   ((kbd "C-x C-b") . ibuffer)
   ((kbd "C-c r k") . browse-kill-ring)
   ((kbd "C-x x b") . emacs-lisp-byte-compile)
   ([(f1)] . delete-other-windows)
    ((quote [C-return]) . eval-last-sexp)
   ((quote [M-f5]) . shell-command )
   ((quote [M-f2]) . (lambda ()
                       (interactive)
                       (funcall 'linum-mode)))
   ("xh" . hide-region-hide)
   ("xs" . hide-region-unhide)
   ("xa" . hide-region-unhide-all)
   ("xr" . toggle-read-only)
   ([(f5)] . my-compile-main-fun)
   ((kbd "C-x x f") . ffap)
   ((kbd "C-c d") . kid-star-dict)
   ((kbd "C-x xd") . kid-sdcv-to-buffer)
   ((quote [67108903]) .  isearch-forward-cur-word) ;;c-'
   ( "\204" . xilbert-delete-current-word) ;;C-M-d
   ((quote [C-backspace]) .  xilbert-delete-to-line-beginning)
   ((kbd "C-c c b") . xilbert-delete-block) ;;
   ((kbd "C-x x b") . browse-kill-ring)
   ((kbd "C-x x d") . xilbert-descibe-fun-at-point)
   ((kbd "C-x x r") . xilbert-run-fun-at-point)
   ((kbd "C-x x o") . run-shell-at-buffer)
   ((kbd "C-/") . join-line)
   ((kbd  "C-c x l" ) . delete-blank-lines)
   ((kbd "C-c x s") . just-one-space)
   ((kbd "C-c x i") . xilbert-insert-block)
   ((kbd "C-c x t") . insert-time)
   ((kbd "s-]") . xilbert-insert-block2)
   ((quote [C-M-return]) .  edebug-defun)
   ((quote [M-f4]) .  repeat-complex-command)
   ((kbd "C-x M-e") . xilbert-kmacro-manage)
   ((kbd "C-c x d") . xilbert-delete-quote)
   )
 )


;;set for cc-mode
(c-set-offset 'friend '-)
(setq c-basic-offset 6) ;;set 6 space to indent
(setq c-default-style '((java-mode . "java")
                        (awk-mode . "awk")
                        (c++-mode . "stroustrup")
                        (c-mode . "k&r")
                        (other . "linux")))

(setq-default c-electric-flag t) ;;when hit ; for { ,reindents the current line
(defconst sort-of-yang-identifier-regexp "[-a-zA-Z0-9_\\.:]*") 
(add-hook
 'c-mode-common-hook
 '(lambda ()
    (setq outline-regexp
          (concat "^ *" sort-of-yang-identifier-regexp " *" 
                  sort-of-yang-identifier-regexp
                  " *{")))) ;;add spport to outline


;;set m file edited  by matlab-mode
 (autoload 'matlab-mode "matlab" "Matlab Editing Mode" t)
 (add-to-list
  'auto-mode-alist
  '("\\.m$" . matlab-mode))
 (setq matlab-indent-function t)
 (setq matlab-shell-command "octave")

;;set folding
(load "folding" 'nomessage 'noerror)
(folding-mode-add-find-file-hook)
(folding-add-to-marks-list 'prolog-mode "%{{{" "%}}}" nil t)
(folding-add-to-marks-list 'html-mode "<!-- {{{ " "<!-- }}} -->" " -->" nil t)

;;end of setting folding

;;set for hs-minor-mode

(defun toggle-selective-display (column)
  (interactive "P")
  (set-selective-display
   (or column
       (unless selective-display
         (1+ (current-column))))))

(defun toggle-hiding (column)
  (interactive "P")
  (if hs-minor-mode
      (if (condition-case nil
              (hs-toggle-hiding)
            (error t))
          (hs-show-all))
    (toggle-selective-display column)))
(add-to-list 'hs-special-modes-alist
             '(c-mode "[\n\t ]*{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning))
(add-to-list 'hs-special-modes-alist
             '(c++-mode "[\n\t ]*{" "}" "/[*/]" nil hs-c-like-adjust-block-beginning))

(add-hook 'c-mode-common-hook   'hs-minor-mode)
(add-hook 'emacs-lisp-mode-hook 'hs-minor-mode)
(add-hook 'lisp-mode-hook       'hs-minor-mode)
(require 'perl-mode)
(require 'cperl-mode)
(add-hook 'perl-mode-hook       'hs-minor-mode)
(add-hook 'cperl-mode-hook      'hs-minor-mode)
(dolist (map (list c-mode-base-map emacs-lisp-mode-map cperl-mode-map perl-mode-map ))
  (define-key map (kbd "C-c h") 'hs-hide-block)
  (define-key map (kbd "C-c H") 'hs-hide-all)
  (define-key map (kbd "C-c e") 'hs-show-block)
  (define-key map (kbd "C-c E") 'hs-show-all))
;;end of setting hs-minor-mode

;;remember mode
(setq remember-annotation-functions '(org-remember-annotation))
 (setq remember-handler-functions '(org-remember-handler))
 (add-hook 'remember-mode-hook 'org-remember-apply-template)
;;(org-remember-insinuate)
(define-key global-map "\C-cr" 'org-remember)
(define-key global-map "\C-cl" 'org-store-link)
;;set templates
(setq org-remember-templates
      '(("备忘录" ?t "* TODO %?\n\n  %i " "D:/廖小卿/文献与文档/笔记/gtd.org" "Reminder")
        ("新想法" ?i  "* TODO %?\n\n %i" "D:/廖小卿/文献与文档/笔记/gtd.org" "New Ideas")
        ; ("临时记录" ?j "* %U %?\n\n  %i" "E:/文献与文档/笔记/临时记录.org" "记录条目")
        )
      )

;;add to agenda
(setq org-deadline-warning-days 14)
(setq org-agenda-files (list  "D:/廖小卿/文献与文档/笔记/事情安排.org"
                              "D:/廖小卿/文献与文档/笔记/note.org"
                              "D:/廖小卿/文献与文档/笔记/gtd.org"
                              ))
;;end

;;当子标题的项目全部为done状态时，父标题自动变为done状态。
(defun org-summary-todo (n-done n-not-done)
  "Swith entry to DONE when all subentries are done, to TODO otherwise."
  (let (org-log-done org-log-states)   ; turn off logging.
    (org-todo (if (= n-not-done 0) "DONE" "TODO"))))

;;set for cperl
(require 'cperl-mode)
(add-to-list 'auto-mode-alist '("\\.\\([pP][Llm]\\|al\\)\\'" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("perl" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("perl5" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("miniperl" . cperl-mode))
(add-hook 'cperl-mode-hook
	  (lambda ()
	    (local-set-key (kbd "C-h f") 'cperl-perldoc)))

(setq cperl-invalid-face (quote off)) ;;disable underline face
(cperl-set-style "K&R")


(require 'inf-perl)
(defalias  'run-perl 'inf-perl-start)
;;(setq perl-shell-program (expand-file-name "~/psh.pl"))

;;pod-mode
(require 'pod-mode)
(add-to-list 'auto-mode-alist '("\\.\\([pP][oO][Dd]\\)\\'" . pod-mode))
;;end

;; run interactively accroding major-mode
(defvar xilbert-inf-hook nil)
(defun xilbert-run-inf ()
  (interactive)
  (run-hooks 'xilbert-inf-hook))
(add-hook 'xilbert-inf-hook
          '(lambda ()
             (let (cmd mode)
               (setq mode major-mode)
               (cond ((or (eq mode 'perl-mode)
                           (eq mode 'cperl-mode))
                      (setq cmd 'run-perl))
                     ((or (eq mode 'octave-mode)
                         (eq mode 'matlab-mode))
                      (setq cmd 'run-octave))
                     ((eq mode 'haskell-mode)
                      (setq cmd 'run-haskell))
                     ((eq mode 'python-mode)
                      (setq cmd 'run-python))
                     ((eq mode 'lisp-mode)
                      (setq cmd 'run-lisp))
                     ((eq mode 'emacs-lisp-mode)
                      (setq cmd 'ielm))
                     ((eq mode 'scheme-mode)
                      (setq cmd 'run-scheme))
                     (t nil))
               (if cmd
               (funcall cmd)))))
               
(global-set-key (quote [f6]) 'xilbert-run-inf)
;;end

;;set for lua-mode
(setq auto-mode-alist (cons '("\\.lua$" . lua-mode) auto-mode-alist))
(autoload 'lua-mode "lua-mode" "Lua editing mode." t)
(add-hook 'lua-mode-hook 'turn-on-font-lock)
(add-hook 'lua-mode-hook hs-minor-mode)

(defcustom ac-modes
  '(emacs-lisp-mode
    lisp-interaction-mode
    c-mode cc-mode c++-mode  java-mode
    perl-mode cperl-mode python-mode 
    javascript-mode   css-mode
    makefile-mode   
    lua-mode muse-mode )
  "Major modes `auto-complete-mode' can run on."
  :type '(repeat symbol)
  :group 'auto-complete)
;;end

;;提升c-c c-f 的速度
(setq tramp-default-method "ftp")

;;按C-c k 键删除选定内容
(global-set-key "k" (quote delete-region))
;;右键拖动选定内容
(define-key global-map [down-mouse-3] 'mouse-drag-region)

;;解决c-c 的问题
;; C-c s-h comment-region
(global-set-key (quote [3 8388712]) (quote comment-region))
;; c-c s-k uncomment-region
(global-set-key (quote [3 8388715]) (quote uncomment-region))
;;c-c M-w  kill-ring-save
(global-set-key "\367" (quote kill-ring-save))
;;c-c tool-bar-save 
(global-set-key (quote [3 (tool-bar)]) (quote clipboard-kill-ring-save))
(global-set-key (kbd "C-c C-M-\\") 'indent-region)
;;binding C-c o to occur-mode
(global-set-key (kbd "C-c o") 'occur)
;;hit C-c s to open shell
(global-set-key "\C-cs" 'shell)
;;use rf command to open emacs-lisp reference
(defalias 'rf 'menu-bar-read-lispref)

;;配置日历

(global-set-key (kbd "<f12>") 'calendar)
;;配置mode-line 上时间显示的格式
(setq display-time-format "%m月%d日 %H点:%M分")
;;设置我所在地方的经纬度，calendar里有个功能是日月食的预测，和你的经纬度相联系的。
;; (setq calendar-latitude +39.9)
;; (setq calendar-longitude +116.4)
;; (setq calendar-location-name "西安")
;;设定一周的开始为周一
(setq calendar-week-start-day 1)
;;节日和生日提醒设置
;;希伯来人的节日和伊斯兰教的节日设为nil
;;我是无神论者，不过我喜欢神话，大家有兴趣也可以探讨一下，发email给我吧
;; (setq christian-holidays t) ;;基督教节日
;; (setq hebrew-holidays nil)
;; (setq islamic-holidays nil)
;;设定一些自定义的生日和节日
;;后面那些农历节日需要每年根据日历修改
;;有些可惜，没有中国传统农历，谁能写个扩展elisp包弄一下
;; (setq general-holidays '((holiday-fixed 1 1 "元旦")
;;                          (holiday-fixed 3 8 "妇女节")
;;                          (holiday-fixed 3 2 "杨欢的生日")
;;                          (holiday-fixed 4 1 "愚人节")
;;                          (holiday-fixed 5 1 "劳动节")
;;                          (holiday-fixed 9 10 "教师节")
;;                          (holiday-fixed 10 1 "国庆节")
;;                          (holiday-fixed 12 25 "圣诞节")
;;                          (holiday-float 5 0 2 "母亲节")
;;                          (holiday-float 6 0 3 "父亲节")
;;                          ))
;;日历不和日记相连，我不用Calendar自带的diary记日记
;; (setq mark-diary-entries-in-calendar nil)
;;在日历中突出标记节日和生日
;; (setq mark-holidays-in-calendar t)
;;打开calendar自动打开节日和生日列表
;; (setq view-calendar-holidays-initially t)
;; (setq calendar-date-style 'iso)
;;中文日历设置

;; (require 'cal-china-x)
;; (setq holiday-xilbert-holidays
;;       '(;;公历节日
;;         (holiday-fixed 1 1 "元旦")
;;         (holiday-fixed 2 14 "情人节")
;;         (holiday-fixed 3 8 "妇女节")
;;         (holiday-fixed 3 14 "白色情人节")
;;         (holiday-fixed 4 1 "愚人节")
;;         (holiday-fixed 5 1 "劳动节")
;;         (holiday-float 5 0 2 "母亲节")
;;         (holiday-fixed 6 1 "儿童节")
;;         (holiday-float 6 0 3 "父亲节")
;;         (holiday-fixed 9 10 "教师节")
;;         (holiday-fixed 10 1 "国庆节")
;;         (holiday-fixed 12 25 "圣诞节")
;;         ;; 农历节日
;;         (holiday-lunar 1 1 "春节" 0)
;;         (holiday-lunar 1 2 "春节" 0)
;;         (holiday-lunar 1 3 "春节" 0)
;;         (holiday-lunar 1 15 "元宵节" 0)
;;         (holiday-solar-term "清明" "清明节")
;;         (holiday-lunar 5 5 "端午节" 0)
;;         (holiday-lunar 8 15 "中秋节" 0)
;;         ;; 生日 -- 家人,朋友
;;         (holiday-fixed 3 2 "欢的生日")
;;         (holiday-lunar 8 5 "我的生日" 0)
;;         ))
;; (setq calendar-holidays holiday-xilbert-holidays)

;;滚屏更连续，而不是每次滚半屏
(setq scroll-step 1
      scroll-margin 1
      scroll-conservatively 10000)
;;set for anything
(require 'anything)
;;--------------------------------------------------------------------------------
;;set for viper
(eval-after-load 'viper
  '(progn
     (require 'vimpulse)
     (setq viper-vi-state-id (concat (propertize "<vi cmd>" 'face 'hi-blue-b) " "))
     (setq viper-emacs-state-id (concat (propertize "<E>" 'face 'hi-red-b) " "))
     (setq viper-insert-state-id (concat (propertize "<I>" 'face 'hi-blue-b) " "))
     (setq viper-replace-state-id (concat (propertize "<R>" 'face 'hi-blue-b) " "))
     ;; The property `risky-local-variable' is a security measure
     ;; for mode line variables that have properties
     (put 'viper-mode-string 'risky-local-variable t)
     (setq viper-expert-level 5)
     (define-key viper-insert-global-user-map [backspace] 'backward-delete-char-untabify)
     )
  )
;;--------------------------------------------------------------------------------

;;for easy-gpg
(require 'epa-file)
(epa-file-enable)
;; 总是使用对称加密
;;(setq epa-file-encrypt-to nil)
;; 允许缓存密码，否则编辑时每次保存都要输入密码
(setq epa-file-cache-passphrase-for-symmetric-encryption t)
;; 允许自动保存
;;(setq epa-file-inhibit-auto-save nil)

;;--------------------------------------------------------------------------------
;;for backup files
;; 所有的备份文件转移到~/backups目录下
(setq backup-directory-alist (quote (("." . "d:/gnu/home/emacs-backups"))))
(setq version-control t)
(setq kept-old-versions 2)
(setq kept-new-versions 5)
(setq delete-old-versions t)
(setq backup-by-copying t)
;; Emacs 中，改变文件时，默认都会产生备份文件(以 ~ 结尾的文件)。可以完全去掉
;; (并不可取)，也可以制定备份的方式。这里采用的是，把所有的文件备份都放在一
;; 个固定的地方("~/var/tmp")。对于每个备份文件，保留最原始的两个版本和最新的
;; 五个版本。并且备份的时候，备份文件是复本，而不是原件。

;;不产生备份文件
;;(setq make-backup-files nil)

;;--------------------------------------------------------------------------------
;;C-/ to join all the lines in region
(defun xilbert-region-join-lines (begin end)
  (interactive "r")
  (save-excursion
    (goto-char begin)
    (while (re-search-forward "[\n]" (- end 4) t)
      (replace-match "" nil nil))))
(global-set-key (kbd "C-/") 'xilbert-region-join-lines)
;;--------------------------------------------------------------------------------
;;just don't display the dos style end of line char
(defun remove-dos-eol () 
  "Removes the disturbing '^M' showing up in files containing mixed UNIX and DOS line endings."   (interactive) 
  (setq buffer-display-table (make-display-table)) 
  (aset buffer-display-table ?\^M []))

;;bind C-c xy to clipboard-kill-ring-save
(global-set-key (kbd "C-c x y") 'clipboard-kill-ring-save)

;;--------------------------------------------------------------------------------
;;set for R
(setq ess-ask-for-ess-directory nil)
(setq ess-local-process-name "R")
(setq ansi-color-for-comint-mode 'filter)
(setq comint-prompt-read-only t)
(setq comint-scroll-to-bottom-on-input t)
(setq comint-scroll-to-bottom-on-output t)
(setq comint-move-point-for-output t)
(defun my-ess-start-R ()
  (interactive)
  (if (not (member "*R*" (mapcar (function buffer-name) (buffer-list))))
      (progn
	(delete-other-windows)
	(setq w1 (selected-window))
	(setq w1name (buffer-name))
	(setq w2 (split-window w1))
	(R)
	(set-window-buffer w2 "*R*")
	(set-window-buffer w1 w1name))))
(defun my-ess-eval ()
  (interactive)
  (my-ess-start-R)
  (if (and transient-mark-mode mark-active)
      (call-interactively 'ess-eval-region)
    (call-interactively 'ess-eval-line-and-step)))
(add-hook 'ess-mode-hook
          '(lambda()
             (local-set-key [(shift return)] 'my-ess-eval)))
(add-hook 'inferior-ess-mode-hook
          '(lambda()
             (local-set-key [C-up] 'comint-previous-input)
             (local-set-key [C-down] 'comint-next-input)))
(require 'ess-site)
(setq-default inferior-R-program-name "R")
;; enable skeleton-pair insert globally
(setq skeleton-pair t)
;;(setq skeleton-pair-on-word t)
;; (global-set-key (kbd "(") 'skeleton-pair-insert-maybe)
(global-set-key (kbd "[") 'skeleton-pair-insert-maybe)
(global-set-key (kbd "{") 'skeleton-pair-insert-maybe)
(global-set-key (kbd "\"") 'skeleton-pair-insert-maybe)
;; (global-set-key (kbd "\'") 'skeleton-pair-insert-maybe)
;; (global-set-key (kbd "\`") 'skeleton-pair-insert-maybe)
;;(global-set-key (kbd "<") 'skeleton-pair-insert-maybe)

;;outline mode for R
(add-hook 'ess-mode-hook
          '(lambda ()
             (outline-minor-mode)
             (setq outline-regexp "\\(^#\\{4,5\\} \\)\\|\\(^[a-zA-Z0-9_\.]+ ?<- ?function(.*{\\)")
             (defun outline-level
               (lambda () (interactive) (cond ((looking-at "^##### ") 1)((looking-at "^#### ") 2)((looking-at "^[a-zA-Z0-9_\.]+ ?<- ?function(.*{") 3) (t 1000)))
               )))
;; lines starting with #####  --> level 1
;;   lines starting with ####   --> level 2
;;   R functions                --> level 3
;;--------------------------------------------------------------------------------
;;configure for gnuplot
;;;; ====  Configure Gnuplot-mode  ====
;; these lines enable the use of gnuplot mode
(autoload 'gnuplot-mode "gnuplot" "gnuplot major mode" t)
(autoload 'gnuplot-make-buffer "gnuplot" "open a buffer in gnuplot mode" t)
 (setq gnuplot-program
       "D:\\gnu\\octave\\bin\\gnuplot.exe")

(setq auto-mode-alist
      (append
       (list
        '("\\.gp$" . gnuplot-mode)
        '("\\.plt$" . gnuplot-mode)
        )
       auto-mode-alist))

;; if you have the latest win32 version of gnuplot
;; (add-hook 'gnuplot-load-hook
;;           '(lambda ()
;;              (setq gnuplot-program
;;                    "C:\\Programme\\gnuplot\\bin\\pgnuplot.exe")
;;              (setq gnuplot-gnuplot-buffer "plot.plt") ; name of a new gnuplot file
;;                                         ; (setq show-trailing-whitespace t)
;;              (setq whitespace-check-buffer-ateol t)
;;              ))

;;--------------------------------------------------------------------------------
;;alias calculation
(defalias 'xcalc  'calculator)
(put 'downcase-region 'disabled nil)

;;bind M-y  to paste clipboard content
(global-set-key (kbd "M-y") 'clipboard-yank)

;;--------------------------------------------------------------------------------
;; 按.el 文件名排序后进行加载
;; (defun liuw-find-el-in-directory (directory)
;;   "List the .el files in DIRECTORY."
;;   ;; This can be made recursive
;;   (interactive "DDirectory: ")
;;   (let (el-files-list
;;         (current-directory-list
;;          (directory-files-and-attributes directory t)))
;;     (while current-directory-list
;;       (if (equal ".el"
;;                  (substring (car (car current-directory-list)) -3))
;;           (setq el-files-list
;;                 (cons (car (car current-directory-list)) el-files-list)))
;;       (setq current-directory-list (cdr current-directory-list)))
;;     el-files-list))

;;  (defun liuw-get-sorted-el-list (directory)
;;    (interactive "DDirectory: ")
;;    (let ((l (sort (liuw-find-el-in-directory directory) 'string<)))
;;      l))

;;  (defun liuw-load-ordered-startup-el (directory)
;;    (interactive "DDirectory: ")
;;    (let ((l (liuw-get-sorted-el-list directory)))
;;      (while l
;;        (load-file (car l))
;;        (setq l (cdr l)))))
;;--------------------------------------------------------------------------------
(add-hook 'org-mode-hook 'turn-on-auto-fill)
(setq default-fill-column 80)
;;打开所有主模式的auto-fill-mode
;;(setq-default auto-fill-function 'do-auto-fill)

;; use things at point lib
;; (require 'thingatpt)

;; using yasnippet
;; (require 'yasnippet-bundle)
;; (require 'yasnippet)
;; (yas/initialize)
;; (yas/load-directory "D:/gnu/Emacs23.1/site-lisp/yasnippet-0.6.1c/snippets/")

;;在一个buffer中单独显示org的标题
(require 'org-toc)









